package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.TianJieFinanceGameDto;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileGameService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_游戏_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/game")
public class TianJieMobileGameController extends BaseController{
    @Autowired
    private TianJieMobileGameService gameService;

    @RequestMapping(value="/getGames",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="获取游戏列表。不分页")
    public void getGames(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            List<TianJieFinanceGameDto> games=gameService.getGames(memberId);
            ResponseMessage.success(games);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/getGames----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/charge",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="游戏充值。（不走第三方支付渠道，直接使用消费卡抵扣）")
    public void charge(HttpServletRequest request,
                          HttpServletResponse response,
                         /* @ApiParam(required = true, name = "chargeAmount", value = "充值金额") @RequestParam(value = "chargeAmount", required = true) BigDecimal chargeAmount,
                       */@ApiParam(required = true, name = "chargeProductId", value = "充值产品Id") @RequestParam(value = "chargeProductId", required = true) Long chargeProductId,


                        @ApiParam(required = true, name = "gameId", value = "游戏ID") @RequestParam(value = "gameId", required = true) Long gameId,
                          @ApiParam(required = true, name = "account", value = "游戏账户") @RequestParam(value = "account", required = true) String account,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
           // gameService.charge(memberId,chargeProductId,gameId,account);
           // ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/game/charge----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
